package com.smartthinking.dive;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Dictionary;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ProgressBar;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.provider.Contacts;


@SuppressWarnings("deprecation")
public class AnunciantesDetalles extends Activity{

	TextView paginaweb, tel1, tel2, tel3, tel4, correo, correo2, desc, texto ;
	String poi_thumb, poi_desc, poi_telefono1, poi_telefono2, poi_telefono3, poi_telefono4, poi_mail, 
	poi_www, poi_facebook, poi_twitter, poi_pdf, poi_lat, poi_lng, poi_direccion, poi_colonia, poi_municipio, poi_cp, cat_cve, poi_idr,cat_des, 
	poi_nombre, poi_youtube, poi_mail2, poi_skype, poi_horarios, sub_cve, edo_cve;
	ImageView imagen, btnpdf, btnskype, btnyoutube, btnfacebook, btntwitter;
	TableRow tabla1, tabla2, tabla3;
	Context context;
	ListView listView4;
	TableLayout tablasocial, tabladatos;
	RelativeLayout detalles;
	private static final String HTTPS = "https://";
	private static final String HTTP = "http://";
	Button btn, home,contacto;
	ProgressBar face;
	String dir, colonia, mucicipio, codigopostal, nombre, telefono1, telefono2, mail1, mail2, direccion;
	
	//	@SuppressLint("SdCardPath")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tarjeta_informacion);
		context = this;
		paginaweb = (TextView) findViewById(R.id.textViewPagina);
		tel1 = (TextView) findViewById(R.id.textViewtel1);
		tel2 = (TextView) findViewById(R.id.textViewtel2);
		tel3 = (TextView) findViewById(R.id.textViewtel3);
		tel4 = (TextView) findViewById(R.id.textViewtel4);
		correo = (TextView) findViewById(R.id.textViewcorreo);
		correo2 = (TextView) findViewById(R.id.textViewcorreo2);
		desc = (TextView) findViewById(R.id.textViewtexteven);
		imagen = (ImageView) findViewById(R.id.imageViewbanner);
		tabla1 = (TableRow) findViewById(R.id.tableRow1);
		tabla2 = (TableRow) findViewById(R.id.tableRow2);
		tabla3 = (TableRow) findViewById(R.id.tableRow3);
		btnpdf = (ImageView) findViewById(R.id.imageView1);
		btnskype = (ImageView) findViewById(R.id.imageView8);
		btnyoutube = (ImageView) findViewById(R.id.imageView9);
		btnfacebook = (ImageView) findViewById(R.id.imageView6);
		btntwitter = (ImageView) findViewById(R.id.imageView7);
		tablasocial = (TableLayout) findViewById(R.id.tableLayoutredsocial);
		tabladatos = (TableLayout) findViewById(R.id.tableLayoutdetalles);
		detalles = (RelativeLayout) findViewById(R.id.relativeLayoutlogo);
		texto= (TextView) findViewById(R.id.textclick);
		home= (Button) findViewById(R.id.btnHOME);
		contacto = (Button) findViewById(R.id.btncontacto);

		home.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				finish();
				Intent intent1 = new Intent(AnunciantesDetalles.this,Principal.class);
				intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent1);
			}
		});

		int tam = Utilities.getScreenSize2(AnunciantesDetalles.this);
		if(tam == 1){
			RelativeLayout.LayoutParams params = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params.setMargins(20, 20, 10, 0);
			//			params.setMargins(left, top, right, bottom)
			detalles.setLayoutParams(params);

			RelativeLayout.LayoutParams params1 = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params1.setMargins(0, 10, 0, 0);
			params1.addRule(RelativeLayout.BELOW, R.id.relativeLayoutlogo);
			tabladatos.setLayoutParams(params1);

			RelativeLayout.LayoutParams params2 = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params2.setMargins(25, 4, 0, 0);
			params2.addRule(RelativeLayout.BELOW, R.id.tableLayoutdetalles);
			tablasocial.setLayoutParams(params2);

		}


		if(tam == 2){
			RelativeLayout.LayoutParams params = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params.setMargins(30, 50, 20, 0);
			//			params.setMargins(left, top, right, bottom)
			detalles.setLayoutParams(params);

			RelativeLayout.LayoutParams params1 = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			
			
			
			int widthOfscreen =0;
			int heightOfScreen = 0;
			DisplayMetrics dm = new DisplayMetrics();
			        try {
			            ((Activity) context).getWindowManager().getDefaultDisplay()
			                    .getMetrics(dm);
			        } catch (Exception ex) {
			        }
			         widthOfscreen  = dm.widthPixels;
			heightOfScreen  = dm.heightPixels;
			
//			Toast toast1 =
//		            Toast.makeText(getApplicationContext(),
//		                    "Getting Width: "+ widthOfscreen+ "Getting Height: " + heightOfScreen, Toast.LENGTH_LONG);
//		        toast1.show();
			Log.d("Tag", "Getting Width >> " + widthOfscreen);
			Log.d("Tag", "Getting Height >> " + heightOfScreen);
			
			if(heightOfScreen>=480 && heightOfScreen<=800){
				
				params1.setMargins(0, 45, 0, 0);
				params1.addRule(RelativeLayout.BELOW, R.id.relativeLayoutlogo);
				tabladatos.setLayoutParams(params1);
				
			}else if(heightOfScreen>=800 && heightOfScreen<=880){
			
				params1.setMargins(0, 70, 0, 0);
				params1.addRule(RelativeLayout.BELOW, R.id.relativeLayoutlogo);
				tabladatos.setLayoutParams(params1);
				
			}else if(heightOfScreen>=880){
				
				params1.setMargins(0, 90, 0, 0);
				params1.addRule(RelativeLayout.BELOW, R.id.relativeLayoutlogo);
				tabladatos.setLayoutParams(params1);
				
			}
			
//			params1.setMargins(0, 45, 0, 0);
//			params1.addRule(RelativeLayout.BELOW, R.id.relativeLayoutlogo);
//			tabladatos.setLayoutParams(params1);

			
			
			
			RelativeLayout.LayoutParams params2 = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
//			params2.setMargins(35, 35, 0, 0);
//			params2.addRule(RelativeLayout.BELOW, R.id.tableLayoutdetalles);
			params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			tablasocial.setPadding(35, 0, 0, 28);
			tablasocial.setLayoutParams(params2);

		}


		if(tam == 3){
			RelativeLayout.LayoutParams params = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params.setMargins(60, 80, 40, 0);
			//			params.setMargins(left, top, right, bottom)
			detalles.setLayoutParams(params);

			RelativeLayout.LayoutParams params1 = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params1.setMargins(25, 160, 0, 0);
			params1.addRule(RelativeLayout.BELOW, R.id.relativeLayoutlogo);
			tabladatos.setLayoutParams(params1);

			RelativeLayout.LayoutParams params2 = 
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);
			params2.setMargins(90, 90, 0, 0);
			params2.addRule(RelativeLayout.BELOW, R.id.tableLayoutdetalles);
			tablasocial.setLayoutParams(params2);

		}






		Bundle extras1 = getIntent().getExtras();
		poi_thumb = extras1.getString("query6");
		poi_desc = extras1.getString("query7");
		poi_telefono1 = extras1.getString("query8");
		poi_telefono2 = extras1.getString("query9");
		poi_telefono3 = extras1.getString("query10");
		poi_telefono4 = extras1.getString("query11");
		poi_mail = extras1.getString("query12");
		poi_www = extras1.getString("query13");
		poi_facebook = extras1.getString("query14");
		poi_twitter = extras1.getString("query15");
		poi_pdf = extras1.getString("query16");
		poi_lat = extras1.getString("query17");
		poi_lng = extras1.getString("query18");
		poi_direccion = extras1.getString("query19");
		poi_colonia = extras1.getString("query20");
		poi_municipio = extras1.getString("query21");
		poi_cp = extras1.getString("query22");
		poi_idr = extras1.getString("query23");
		poi_nombre = extras1.getString("query24");

		poi_youtube = extras1.getString("query60");
		poi_mail2 = extras1.getString("query62");
		poi_skype = extras1.getString("query61");
		poi_horarios = extras1.getString("query63");
		edo_cve = extras1.getString("query64");
		
		final StringBuilder builder = new StringBuilder();
		
		if(poi_nombre.equals("")||poi_nombre.equals("null")||poi_nombre.equals(null)){
			nombre = "";
		}else {
			nombre = poi_nombre;
		}
		
		if(poi_mail2.equals("")||poi_mail2.equals("null")||poi_mail2.equals(null)){
			mail2 = "";
		}else {
			mail2 = poi_mail2;
		}

		if(poi_mail.equals("")||poi_mail.equals("null")||poi_mail.equals(null)){
			mail1 = "";
		}else {
			mail1 = poi_mail;
		}
		
		if(poi_direccion.equals("")||poi_direccion.equals("null")||poi_direccion.equals(null)){
			dir = "";
			builder.append(dir);
		}else {
			dir = poi_direccion;
			builder.append(dir);
		}


		if(poi_colonia.equals("")||poi_colonia.equals("null")||poi_colonia.equals(null)){
			colonia = "";
			builder.append(colonia);
		}else {
			colonia = " "+poi_colonia;
			builder.append(colonia);
		}


		if(poi_municipio.equals("")||poi_municipio.equals("null")||poi_municipio.equals(null)){
			 mucicipio = "";
			 builder.append(mucicipio);
		}else {
			mucicipio = " "+poi_municipio;
			builder.append(mucicipio);
		}
		
		if(poi_cp.equals("")||poi_cp.equals("null")||poi_cp.equals(null)){
			codigopostal = "";
			builder.append(codigopostal);
		}else {
			codigopostal = " C.P."+poi_cp;
			builder.append(codigopostal);
		}
		
		if(poi_telefono1.equals("")||poi_telefono1.equals("null")||poi_telefono1.equals(null)){
			telefono1 = "";
		}else {
			telefono1 = poi_telefono1;
		}
		
		if(poi_telefono2.equals("")||poi_telefono2.equals("null")||poi_telefono2.equals(null)){
			telefono2 = "";
		}else {
			telefono2 = poi_telefono2;
		}
		builder.toString();
		
		contacto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent addContactIntent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
				addContactIntent.putExtra(Contacts.Intents.Insert.NAME, nombre); 
				addContactIntent.putExtra(Contacts.Intents.Insert.PHONE, telefono1);
				addContactIntent.putExtra(Contacts.Intents.Insert.EMAIL, mail1);
				addContactIntent.putExtra(Contacts.Intents.Insert.SECONDARY_EMAIL, mail2);
				addContactIntent.putExtra(Contacts.Intents.Insert.SECONDARY_PHONE, telefono2);
				addContactIntent.putExtra(Contacts.Intents.Insert.POSTAL, builder.toString());
				startActivity(addContactIntent);
			}
		});

		ArrayList<Dictionary<String, String> > listacat = null;
		ArrayList<Dictionary<String, String> > listacatdes = null;
		ArrayList<Dictionary<String, String> > listasub = null;

		String url5="select * from pois_subcategorias where poi_idr= "+poi_idr+" ";
		listacat = Utilities.qry2(getApplicationContext(),url5);

		final StringBuilder sb = new StringBuilder();
		for(int i = 0; i< listacat.size(); i++){
			cat_cve = listacat.get(i).get("cat_cve");
			String url6="select * from categorias where cat_cve= "+cat_cve+"";
			Log.i("URL7", ""+url6);
			listacatdes = Utilities.qry2(getApplicationContext(),url6);
			
//			if(listacatdes.size() > 0){
			for(int j = 0; j < listacatdes.size(); j++){
				sb.append(listacatdes.get(j).get("cat_des")+" ");
				sub_cve = listacat.get(i).get("sub_cve");
				String url7="select * from subcategorias where sub_cve= "+sub_cve+" and cat_cve= "+cat_cve+"";
				Log.i("URL7", ""+url7);
				listasub = Utilities.qry2(getApplicationContext(),url7);
				sb.append("("+listasub.get(0).get("sub_des")+") ");
				
			}
//				sb.append(listacatdes.get(i).get("cat_des")+" ");

//				sub_cve = listacat.get(i).get("sub_cve");
//				String url7="select * from subcategorias where sub_cve= "+sub_cve+" and cat_cve= "+cat_cve+"";
//				Log.i("URL7", ""+url7);
//				listasub = Utilities.qry2(getApplicationContext(),url7);
//
//				sb.append("("+listasub.get(i).get("sub_des")+")");
//			}
			

		}
		Log.i("LISTA", ""+sb.toString());
		tel1.setText(poi_telefono1+"	  ");
		tel2.setText(poi_telefono2+"	  ");
		tel3.setText(poi_telefono3+"	  ");
		tel4.setText(poi_telefono4);

		String edo = "";

		if(edo_cve.equals("1")){
			edo="AGUASCALIENTES";
		}
		if(edo_cve.equals("2")){
			edo="BAJA CALIFORNIA";
		}
		if(edo_cve.equals("3")){
			edo="BAJA CALIFORNIA SUR";
		}
		if(edo_cve.equals("4")){
			edo="CAMPECHE";
		}
		if(edo_cve.equals("5")){
			edo="CHIAPAS";
		}
		if(edo_cve.equals("6")){
			edo="CHIHUAHUA";
		}

		if(edo_cve.equals("7")){
			edo="COAHUILA";
		}

		if(edo_cve.equals("8")){
			edo="COLIMA";
		}

		if(edo_cve.equals("9")){
			edo="DISTRITO FEDERAL";
		}

		if(edo_cve.equals("10")){
			edo="DURANGO";
		}

		if(edo_cve.equals("11")){
			edo="ESTADO DE MEXICO";
		}

		if(edo_cve.equals("12")){
			edo="GUANAJUATO";
		}

		if(edo_cve.equals("13")){
			edo="GUERRERO";
		}

		if(edo_cve.equals("14")){
			edo="HIDALGO";
		}

		if(edo_cve.equals("15")){
			edo="JALISCO";
		}

		if(edo_cve.equals("16")){
			edo="MICHOACAN";
		}

		if(edo_cve.equals("17")){
			edo="MORELOS";
		}

		if(edo_cve.equals("18")){
			edo="NAYARIT";
		}

		if(edo_cve.equals("19")){
			edo="NUEVO LEON";
		}

		if(edo_cve.equals("20")){
			edo="OAXACA";
		}

		if(edo_cve.equals("21")){
			edo="PUEBLA";
		}

		if(edo_cve.equals("22")){
			edo="QUERETARO";
		}

		if(edo_cve.equals("23")){
			edo="QUINTANA ROO";
		}

		if(edo_cve.equals("24")){
			edo="SAN LUIS POTOSI";
		}

		if(edo_cve.equals("25")){
			edo="SINALOA";
		}

		if(edo_cve.equals("26")){
			edo="SONORA";
		}

		if(edo_cve.equals("27")){
			edo="TABASCO";
		}

		if(edo_cve.equals("28")){
			edo="TAMAULIPAS";
		}

		if(edo_cve.equals("29")){
			edo="TLAXCALA";
		}

		if(edo_cve.equals("30")){
			edo="VERACRUZ";
		}

		if(edo_cve.equals("31")){
			edo="YUCATAN";
		}

		if(edo_cve.equals("32")){
			edo="ZACATECAS";
		}


		paginaweb.setText(poi_www);

		if((poi_mail2.equals(null))||(poi_mail2.equals("null"))||(poi_mail2.equals(""))){
			correo.setText(poi_mail);
			
		}else{
			correo.setText(poi_mail);
			correo2.setText(poi_mail2);
		}



				if((poi_direccion.equals(null))||(poi_direccion.equals("null"))
					||(poi_direccion.equals(""))||(poi_municipio.equals(null))||(poi_municipio.equals("null"))||(poi_municipio.equals("")
					||(poi_cp.equals(null))||(poi_cp.equals("null"))||(poi_cp.equals("")))){
					
					desc.setText(poi_nombre+"\n\n"+sb+"\n\n"+poi_desc+"\n\n"+edo);
					desc.setMovementMethod(new ScrollingMovementMethod());
				}else{
		desc.setText(poi_nombre+"\n\n"+sb+"\n\n"+poi_desc+"\n\n"+poi_direccion+" "+","+poi_colonia+" "+","+poi_municipio+" "+","+poi_cp+"\n\n"+edo);
		desc.setMovementMethod(new ScrollingMovementMethod());
				}


		File storagePath = Environment.getExternalStorageDirectory();
		Uri uri=Uri.parse(getApplicationContext().getFilesDir()+"/localdata/"+poi_thumb);

//		Uri uri=Uri.parse(storagePath+"/Squenda/"+poi_thumb);
		imagen.setImageURI(uri);

		texto.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				Intent intent = new Intent(AnunciantesDetalles.this,TextoDetallesAnunciantes.class);
				intent.putExtra("query25", ""+sb);
				intent.putExtra("query7", ""+poi_desc);
				intent.putExtra("query19", ""+poi_direccion);
				intent.putExtra("query20", ""+poi_colonia);
				intent.putExtra("query21", ""+poi_municipio);
				intent.putExtra("query22", ""+poi_cp);
				intent.putExtra("query24", ""+poi_nombre);
				intent.putExtra("query63", ""+poi_horarios);
				intent.putExtra("query64", ""+edo_cve);
				intent.putExtra("correo1", ""+poi_mail);
				intent.putExtra("correo2", ""+poi_mail2);
				/*******/

				startActivity(intent);
			}
		});


		tabla1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				//				if (!poi_www.startsWith(HTTP) && !poi_www.startsWith(HTTPS)) {
				//					poi_www = HTTP + poi_www;
				//				}
				//				String url = ""+poi_www;
				//				Log.i("pagina web", ""+poi_www);
				//				Intent intent = new Intent(Intent.ACTION_VIEW);
				//				intent.setData(Uri.parse(url));
				//				startActivity(intent);

				Intent intent = new Intent(AnunciantesDetalles.this,PaginawebAnunciantes.class);
				intent.putExtra("query13", ""+poi_www);
				startActivity(intent);

				////				Uri uri = Uri.parse(poi_www);
				//				Uri uri = Uri.parse("https://squenda.com.mx/");
				//				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				//				startActivity(intent);
			}
		});


		tabla2.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				final Dialog dialog = new Dialog(AnunciantesDetalles.this);
				dialog.setContentView(R.layout.lista_telefonos_anun);
				dialog.setTitle("Selecciona un Teléfono:");

				final String[] datos = new String[]{poi_telefono1, poi_telefono2, poi_telefono3, poi_telefono4}; 

				ArrayAdapter<String> adaptador = new ArrayAdapter<String>(AnunciantesDetalles.this,
						android.R.layout.simple_list_item_1, datos);


				listView4 = (ListView)dialog.findViewById(R.id.listViewtelanun);

				listView4.setAdapter(adaptador);

				listView4.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> a, View v, int position,
							long id) {

						if(poi_telefono1.equals(datos[position]+ ""))
							try {
								String numero = traerNumero(poi_telefono1);
								Intent intent1 = new Intent(Intent.ACTION_CALL);
								intent1.setData(Uri.parse("tel:"+numero));
								startActivity(intent1);
							} catch (ActivityNotFoundException activityException) {
							}

						if(poi_telefono2.equals(datos[position]+ ""))
							try {
								String numero = traerNumero(poi_telefono2);
								Intent intent1 = new Intent(Intent.ACTION_CALL);
								intent1.setData(Uri.parse("tel:"+numero));
								startActivity(intent1);
							} catch (ActivityNotFoundException activityException) {
							}

						if(poi_telefono3.equals(datos[position]+ ""))
							try {
								String numero = traerNumero(poi_telefono3);
								Intent intent1 = new Intent(Intent.ACTION_CALL);
								intent1.setData(Uri.parse("tel:"+numero));
								startActivity(intent1);
							} catch (ActivityNotFoundException activityException) {
							}

						if(poi_telefono4.equals(datos[position]+ ""))
							try {
								String numero = traerNumero(poi_telefono4);
								Intent intent1 = new Intent(Intent.ACTION_CALL);
								intent1.setData(Uri.parse("tel:"+numero));
								startActivity(intent1);
							} catch (ActivityNotFoundException activityException) {
							}

						dialog.dismiss();

					}


				});
				dialog.show();
			}
		});

		tabla3.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0)
			{
				//				String[] to = {poi_mail};
				//				Intent emailIntent = new Intent(Intent.ACTION_SEND);
				//				emailIntent.setData(Uri.parse("mailto:"));
				//				emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
				//				emailIntent.putExtra(Intent.EXTRA_CC, "");
				//				emailIntent.putExtra(Intent.EXTRA_SUBJECT, "");
				//				emailIntent.putExtra(Intent.EXTRA_TEXT, "");
				//				emailIntent.setType("message/rfc822");
				//				startActivity(Intent.createChooser(emailIntent, "Email "));


				final Dialog dialog = new Dialog(AnunciantesDetalles.this);
				dialog.setContentView(R.layout.lista_telefonos_anun);
				dialog.setTitle("Selecciona un Correo:");

				final String[] datos = new String[]{poi_mail, poi_mail2}; 

				ArrayAdapter<String> adaptador = new ArrayAdapter<String>(AnunciantesDetalles.this,
						android.R.layout.simple_list_item_1, datos);


				listView4 = (ListView)dialog.findViewById(R.id.listViewtelanun);

				listView4.setAdapter(adaptador);

				listView4.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> a, View v, int position,
							long id) {

						if(poi_mail.equals(datos[position]+ "")){
							String[] to = {poi_mail};
							Intent emailIntent = new Intent(Intent.ACTION_SEND);
							emailIntent.setData(Uri.parse("mailto:"));
							emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
							emailIntent.putExtra(Intent.EXTRA_CC, "");
							emailIntent.putExtra(Intent.EXTRA_SUBJECT, "");
							emailIntent.putExtra(Intent.EXTRA_TEXT, "");
							emailIntent.setType("message/rfc822");
							startActivity(Intent.createChooser(emailIntent, "Email "));
						}

						if(poi_mail2.equals(datos[position]+ "")){
							String[] to = {poi_mail2};
							Intent emailIntent = new Intent(Intent.ACTION_SEND);
							emailIntent.setData(Uri.parse("mailto:"));
							emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
							emailIntent.putExtra(Intent.EXTRA_CC, "");
							emailIntent.putExtra(Intent.EXTRA_SUBJECT, "");
							emailIntent.putExtra(Intent.EXTRA_TEXT, "");
							emailIntent.setType("message/rfc822");
							startActivity(Intent.createChooser(emailIntent, "Email "));
						}

						dialog.dismiss();

					}


				});
				dialog.show();


			}
		});

		btnpdf.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				//				if (!poi_pdf.startsWith(HTTP) && !poi_pdf.startsWith(HTTPS)) {
				//					poi_pdf = HTTP + poi_pdf;
				//				}

				//				String url = ""+poi_facebook;
				//				Log.i("pagina web", ""+poi_facebook);
				//				Intent intent = new Intent(Intent.ACTION_VIEW);
				//				intent.setData(Uri.parse(url));
				//				startActivity(intent);
				if((poi_pdf.equals(("null"))||(poi_pdf.equals(""))||(poi_pdf.equals(null)))){

					AlertDialog.Builder ad = new AlertDialog.Builder(AnunciantesDetalles.this);
					ad.setMessage("No hay PDF");
					ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
					ad.show();

				}else{
					AlertDialog.Builder builder = new AlertDialog.Builder(AnunciantesDetalles.this);
					builder.setTitle("Aviso");
					builder.setMessage(R.string.accion_a_realizar);
					builder.setPositiveButton("Enviar a Correo", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							String[] to = {};
							Intent emailIntent = new Intent(Intent.ACTION_SEND);
							try {
								InputStream in = new FileInputStream(getApplicationContext().getFilesDir()+"/localdata/"+poi_pdf);
								OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/informacion.pdf" );
								copyFile(in, out);
					              in.close();
					              in = null;
					              out.flush();
					              out.close();
					              out = null;
							    File fileenvio = new File(Environment.getExternalStorageDirectory().toString() +"/informacion.pdf");
							    emailIntent.setData(Uri.parse("mailto:"));
								emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
								emailIntent.putExtra(Intent.EXTRA_CC, "");
								emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Enviar PDF");
								emailIntent.putExtra(Intent.EXTRA_TEXT, "Quiero que me manden el PDF");
								emailIntent.setType("application/pdf");
								emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileenvio));
								startActivity(Intent.createChooser(emailIntent, "Email "));
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
					builder.setNegativeButton("Visualizar PDF", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {

							try{
								InputStream in = new FileInputStream(getApplicationContext().getFilesDir()+"/localdata/"+poi_pdf);
								OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory().toString() +"/informacion.pdf" );
								copyFile(in, out);
					              in.close();
					              in = null;
					              out.flush();
					              out.close();
					              out = null;
					              
					              
					              File file = new File(Environment.getExternalStorageDirectory().toString() +"/informacion.pdf");
									if (file.exists()) { 
										Uri path = Uri.fromFile(file); 
										Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
										pdfIntent.setDataAndType(path, "application/pdf");
										pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

										try{
											context.startActivity(pdfIntent);
										}catch(ActivityNotFoundException e){
											Toast.makeText(context, R.string.st_no_application_available_to_view_pdf, Toast.LENGTH_LONG).show();
										}
									}
					              
							}catch(Exception e){
								e.printStackTrace();
							}
							
						}
					});
					AlertDialog alertDialog = builder.create();
					alertDialog.show();

				}
			}
		});

		//		btnskype btnyoutube poi_youtube query60


		btnskype.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				if((poi_skype.equals(("null"))||(poi_skype.equals(""))||(poi_skype.equals(null)))){
					AlertDialog.Builder ad = new AlertDialog.Builder(AnunciantesDetalles.this);
					ad.setMessage("No hay Skype");
					ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
					ad.show();
				}else{
					try {
					Intent skype_intent = new Intent("android.intent.action.VIEW");
					skype_intent.setClassName("com.skype.raider", "com.skype.raider.Main");
					skype_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					//					skype_intent.setData(Uri.parse("skype:tlakaelel21?chat")); 
					skype_intent.setData(Uri.parse("skype:"+poi_skype+"?chat"));
					context.startActivity(skype_intent);
					
					} catch (ActivityNotFoundException e) {
						AlertDialog.Builder ad = new AlertDialog.Builder(AnunciantesDetalles.this);
						ad.setMessage("No hay aplicación de Skype");
						ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
						ad.show();
				    }
					
				}
			}
		});


		btnyoutube.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if((poi_youtube.equals(("null"))||(poi_youtube.equals(""))||(poi_youtube.equals(null)))){

					AlertDialog.Builder ad = new AlertDialog.Builder(AnunciantesDetalles.this);
					ad.setMessage(R.string.sin_youtube);
					ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
					ad.show();

				}else{
//					Intent intent = new Intent(AnunciantesDetalles.this,PaginaYoutube.class);
//					intent.putExtra("query60", ""+poi_youtube);
//					startActivity(intent);
					try {
				        Intent intent =new Intent(Intent.ACTION_VIEW);
				        intent.setPackage("com.google.android.youtube");
				        intent.setData(Uri.parse("http://www.youtube.com/user/"+poi_youtube+""));
//				        intent.setData(Uri.parse("http://www.youtube.com/user/codigofacilito"));
				        startActivity(intent);
				    } catch (ActivityNotFoundException e) {
				        Intent intent = new Intent(Intent.ACTION_VIEW);
				        intent.setData(Uri.parse("http://www.youtube.com/user/"+poi_youtube+""));
				        startActivity(intent);
				    }
				}
			}
		});




		btnfacebook.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//				try{Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/100002782405641"));
				//				startActivity(intent);
				//				}catch(Exception e){
				//					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/joseluis.cadenasegura")));
				//				}
				if((poi_facebook.equals(("null"))||(poi_facebook.equals(""))||(poi_facebook.equals(null)))){

					AlertDialog.Builder ad = new AlertDialog.Builder(AnunciantesDetalles.this);
					ad.setMessage(R.string.sin_facebook);
					ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
					ad.show();

				}else{
					try {
						context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
						//				    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<id_here>"));
						Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(poi_facebook));
						startActivity(intent);
					} catch (Exception e) {
						startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/"+poi_facebook+"")));
					}
				}
			}
		});



		btntwitter.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//				try
				//				{
				//				    // Check if the Twitter app is installed on the phone.
				//				    getPackageManager().getPackageInfo("com.twitter.android", 0);
				//
				//				    Intent intent = new Intent(Intent.ACTION_VIEW);
				//				    intent.setClassName("com.twitter.android", "com.twitter.android.ProfileActivity");
				//				    // Don't forget to put the "L" at the end of the id.
				////				    intent.putExtra("user_id", 170779083L);//424756310 ferchosolo1
				//				    intent.putExtra("screen-name", "Arthur3507L");
				//				    startActivity(intent);
				//				}
				//				catch (NameNotFoundException e)
				//				{
				//				    // If Twitter app is not installed, start browser.
				//				    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/Arthur3507")));
				//				}
				if((poi_twitter.equals(("null"))||(poi_twitter.equals(""))||(poi_twitter.equals(null)))){

					AlertDialog.Builder ad = new AlertDialog.Builder(AnunciantesDetalles.this);
					ad.setMessage(R.string.sin_twitter);
					ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
					ad.show();

				}else{
					try {
						Intent intent = new Intent(Intent.ACTION_VIEW,
								Uri.parse("twitter://user?screen_name= "+poi_twitter+""));
						startActivity(intent);

					}catch (Exception e) {
						startActivity(new Intent(Intent.ACTION_VIEW,
								Uri.parse("https://twitter.com/#!/"+poi_twitter+""))); 
					} 
				}
			}

		});


	}		


	private String traerNumero(String poi_telefono12) {
		char[] chararray = poi_telefono12.toCharArray();
		StringBuilder builder = new StringBuilder();
		for(int i = 0; i<chararray.length; i++){
			if((chararray[i] == '0')||(chararray[i] == '1')||(chararray[i] == '2')||(chararray[i] == '3')||(chararray[i] == '4')
					||(chararray[i] == '5')||(chararray[i] == '6')||(chararray[i] == '7')||(chararray[i] == '8')||(chararray[i] == '9')){
				builder.append(chararray[i]);
			}
		}
		return builder.toString();
	}

	//	public void BtnFcebook(View view) {
	//
	//		if((poi_facebook.equals(("null"))||(poi_facebook.equals(""))||(poi_facebook.equals(null)))){
	//
	//			AlertDialog.Builder ad = new AlertDialog.Builder(this);
	//			ad.setMessage("No hay p�gina de Facebook");
	//			ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	//				public void onClick(DialogInterface dialog, int id) {
	//					dialog.cancel();
	//				}
	//			});
	//			ad.show();
	//
	//		}else{
	//			Intent intent = new Intent(AnunciantesDetalles.this,PaginaFacebookAnun.class);
	//			intent.putExtra("query14", ""+poi_facebook);
	//			startActivity(intent);

	//			if (!poi_facebook.startsWith(HTTP) && !poi_facebook.startsWith(HTTPS)) {
	//				poi_facebook = HTTP + poi_facebook;
	//			}
	//			String url = ""+poi_facebook;
	//			Log.i("pagina web", ""+poi_facebook);
	//			Intent intent = new Intent(Intent.ACTION_VIEW);
	//			intent.setData(Uri.parse(url));
	//			startActivity(intent);

	//		Uri uri = Uri.parse(poi_facebook);
	//		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
	//		startActivity(intent);

	//		}
	//	}

	//	public void BtnTwitter(View view) {
	//		if((poi_twitter.equals(("null"))||(poi_twitter.equals(""))||(poi_twitter.equals(null)))){
	//
	//			AlertDialog.Builder ad = new AlertDialog.Builder(this);
	//			ad.setMessage("No hay p�gina de Twitter");
	//			ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	//				public void onClick(DialogInterface dialog, int id) {
	//					dialog.cancel();
	//				}
	//			});
	//			ad.show();
	//
	//		}else{
	//			Intent intent = new Intent(AnunciantesDetalles.this,PaginaTwitterAnun.class);
	//			intent.putExtra("query15", ""+poi_twitter);
	//			startActivity(intent);

	//			if (!poi_twitter.startsWith(HTTP) && !poi_twitter.startsWith(HTTPS)) {
	//				poi_twitter = HTTP + poi_twitter;
	//			}
	//			String url = ""+poi_twitter;
	//			Log.i("pagina web", ""+poi_twitter);
	//			Intent intent = new Intent(Intent.ACTION_VIEW);
	//			intent.setData(Uri.parse(url));
	//			startActivity(intent);

	//		Uri uri = Uri.parse(poi_twitter);
	//		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
	//		startActivity(intent);

	//		}
	//	}

    private void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
          out.write(buffer, 0, read);
        }
    }
    
	public void BtnMapa(View view) {

		if((poi_lat.equals(("null"))||(poi_lat.equals(""))||(poi_lat.equals(null))
				||(poi_lng.equals(("null"))||(poi_lng.equals(""))||(poi_lng.equals(null))))){

			AlertDialog.Builder ad = new AlertDialog.Builder(this);
			ad.setMessage("No hay Mapa");
			ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			ad.show();

		}else{
			Intent intent1 = new Intent(AnunciantesDetalles.this,MapaAnunciantes.class);
			intent1.putExtra("query17", ""+poi_lat);
			intent1.putExtra("query18", ""+poi_lng);
			startActivity(intent1);
		}
	}

	public static Bitmap getBitmapFromURL(String src) {
		try {
			URL url = new URL(src);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream input = connection.getInputStream();
			Bitmap myBitmap = BitmapFactory.decodeStream(input);
			return myBitmap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	} 

}


